﻿Imports System.Data.OleDb
Public Class frmChangeLoginDetails

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim con As OleDb.OleDbConnection

        Dim cmd2, mystr As String

        mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                 "Data Source=|DataDirectory|\PsychologicalStateTracker.mdb")

        con = New OleDb.OleDbConnection(mystr)

        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand( _
                   "SELECT * FROM tblLogin WHERE LoginPassword = '" & txtCurrentPassword.Text & "'", con)

        Dim sdr As OleDbDataReader = cmd.ExecuteReader()
        ' If the record can be queried, it means passing verification.   
        If (sdr.Read() = True) Then

            cmd2 = "UPDATE tblLogin SET LoginUsername = '" & txtNewUsername.Text & "'," _
                & "LoginPassword = '" & txtNewPassword.Text & "'"

            Dim run = New OleDb.OleDbCommand

            Try

                run = New OleDbCommand(cmd2, con)

                run.ExecuteNonQuery()

                MsgBox("The Username and Password have been changed succesfully.")

                con.Close()

            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error")

            End Try
        Else
            MessageBox.Show("Invalid current password!")
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        frmLogin.Show()
        Me.Hide()
    End Sub
End Class